<template>
  <div class="app-container">


    <el-table
      v-if="refreshTable"
      v-loading="loading"
      lazy
      :load="load"
      :data="regionList"
      row-key="regionid"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="regionid" label="地区编号" ></el-table-column>
      <el-table-column prop="name" label="地区名称" ></el-table-column>
      <el-table-column prop="activaStatus" label="激活状态" >
        <template slot-scope="scope">
          {{scope.row.activaStatus == 1 ? '已激活' : '未激活'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.level == 0 && scope.row.activaStatus == 0|| scope.row.level == 1 && scope.row.activaStatus == 0"
            size="mini"
            type="text"
            @click="handleDelete(scope.row, '1')"
            v-hasPermi="['system:region:activa']"
          >激活</el-button>
          <el-button
            v-if="scope.row.level == 0 && scope.row.activaStatus == 1|| scope.row.level == 1 && scope.row.activaStatus == 1 "
            size="mini"
            type="text"
            @click="handleDelete(scope.row, '0')"
            v-hasPermi="['system:region:activa']"
          >取消激活</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保险地区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级" prop="parentId">
              <el-input v-model="form.parentName" placeholder="" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地区编号" prop="regionid">
          <el-input v-model="form.regionid" placeholder="请输入地区编号" />
          <div class="hit">注：编码以“上级编码”开头，且编码位数为省级2位，市级4位，县级6位，乡级9位，村级12位</div>
        </el-form-item>
        <el-form-item label="地区名称" prop="regionname">
          <el-input v-model="form.regionname" placeholder="请输入地区名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  addRegion, updateRegion, activationRegion, listAllRegion, setActive } from "@/api/system/region";
export default {
  name: "Region",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险地区表格数据
      regionList: [],
      // 部门树选项
      regionOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionname: null,
        parentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        regionid:[
          { required: true, message: "地区编号不能为空", trigger: "blur" }
        ],
        regionname:[
          { required: true, message: "地区名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList(0);
  },
  methods: {
     /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 查询保险地区列表 */
    getList(pid) {
      this.regionList = []
      this.loading = true;
      listAllRegion(pid).then(response => {
        console.log(response)
        this.regionList = response.data;
        this.loading = false;
      });
    },
    load(tree, treeNode, resolve) {
      console.log(tree,treeNode)
      listAllRegion(tree.regionid).then(response => {
        console.log(response)
        resolve(response.data)
        this.loading = false;
      });
    },
    handleDelete(row, status){
      setActive(row.regionid, status).then(res=>{
        console.log(res)
        if(res.code == 200){
          this.$modal.msgSuccess(`${status == 0? '取消激活':'激活'}成功`);
          this.getList(0)
        }
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.regionid,
        label: node.regionname,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        regionid: null,
        regionname: null,
        parentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    submitForm(){},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.regionid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.parentId = row.regionid
      this.form.parentName = row.regionname
      this.open = true;
      this.title = "添加保险地区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const regionid = row.regionid || this.ids
      this.form = row
      this.regionList.forEach(item=>{
        if(item.regionid == row.parentId){
          this.form.parentName = item.regionname
        }
      })
      this.open = true;
      this.title = "修改保险地区";

    },
  }
};
</script>
<style scoped>
.hit{
  font-size: 12px;
  color: #666;
  line-height: 20px;
  margin-top: 10px;
}
</style>
